﻿using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.Serialization;


namespace ConfExpert.Utility
{
    class XML
    {
        public static string Serialize(params DataTable[] tables)
        {
            //XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            //ns.Add(String.Empty, String.Empty);
            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
            XmlTextWriter tw = new DevTeam.DataManager.KarbelXmlWriter(sw);

            TailordDS.AppDS ds1 = new TailordDS.AppDS();
            //ds1.EnforceConstraints = false;
            foreach (DataTable dt in tables)
                if (dt.GetChanges() != null)
                {
                    ds1.Merge(dt.GetChanges());

                    DataTable hedef = ds1.Tables[dt.TableName];
                    for (int i = hedef.Columns.Count - 1; i >= 0; i--)
                    {
                        if (hedef.Columns[i].DataType == typeof(System.Byte[]))
                            hedef.Columns.RemoveAt(i);
                        else if (hedef.Columns[i].DataType == typeof(DateTime))
                            hedef.Columns[i].DateTimeMode = DataSetDateTime.Unspecified;
                    }

                    //TS yi de remove ediyor. KST için yan etkileri olur mu, bak!
                }

            ds1.WriteXml(tw, XmlWriteMode.IgnoreSchema);
            return sw.ToString();
        }

        public static string SerializeAll(params DataTable[] tables)
        {
            //XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            //ns.Add(String.Empty, String.Empty);
            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
            XmlTextWriter tw = new DevTeam.DataManager.KarbelXmlWriter(sw);

            TailordDS.AppDS ds1 = new TailordDS.AppDS();
            ds1.EnforceConstraints = false; //ERR satırlarında TalepLinesId null diye..
            foreach (DataTable dt in tables)
            {
                ds1.Merge(dt);

                DataTable hedef = ds1.Tables[dt.TableName];
                for (int i = hedef.Columns.Count - 1; i >= 0; i--)
                {
                    if (hedef.Columns[i].DataType == typeof(System.Byte[]))
                        hedef.Columns.RemoveAt(i);
                    else if (hedef.Columns[i].DataType == typeof(DateTime))
                        hedef.Columns[i].DateTimeMode = DataSetDateTime.Unspecified;
                }
            }

            ds1.WriteXml(tw, XmlWriteMode.IgnoreSchema);
            return sw.ToString();
        }

        public static string SerializePic(params DataTable[] tables)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add(String.Empty, String.Empty);
            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
            XmlTextWriter tw = new DevTeam.DataManager.KarbelXmlWriter(sw);

            TailordDS.AppDS ds1 = new TailordDS.AppDS();
            foreach (DataTable dt in tables)
                if (dt.GetChanges() != null)
                    ds1.Merge(dt.GetChanges());

            ds1.WriteXml(tw, XmlWriteMode.IgnoreSchema);
            return sw.ToString();
        }

       

        ////TODO: burası düzgün çalışmıyor!!!
        //public static DataTable MyDeserialize(string xml)
        //{
        //    //DataSet ds = new DataSet();
        //    //XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        //    //ns.Add(String.Empty, String.Empty);
        //    //StringReader sw = new StringReader(xml);
        //    //XmlTextReader tw = new DataManager.MyXmlTextReader(sw);
        //    //ds.ReadXml(tw, XmlReadMode.IgnoreSchema);
        //    //return ds.Tables[0];


        //    /*
        //    // Create an instance of the XmlSerializer specifying type and namespace.
        //    XmlSerializer serializer = new
        //    XmlSerializer(typeof(OrderedItem));

        //    // A FileStream is needed to read the XML document.
        //    FileStream fs = new FileStream(filename, FileMode.Open);
        //    XmlReader reader = new XmlTextReader(fs);

        //    // Declare an object variable of the type to be deserialized
        //    // and use the Deserialize method to restore the object's state.
        //    OrderedItem i = (OrderedItem)serializer.Deserialize(reader);
        //    */


        //    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        //    ns.Add(String.Empty, String.Empty);
        //    XmlSerializer ser = new XmlSerializer(typeof(DataTable));
        //    StringReader stringReader = new StringReader(xml);
        //    XmlTextReader xmlReader = new XmlTextReader(stringReader);
        //    DataTable dt = ser.Deserialize(xmlReader) as DataTable;
        //    xmlReader.Close();
        //    stringReader.Close();
        //    return dt;
        //}

        public static string MySerialize(DataTable dt)
        {
            dt.TableName = "Table1";
            foreach (DataColumn column in dt.Columns)
                if (column.DataType == typeof(DateTime))
                    column.DateTimeMode = DataSetDateTime.Unspecified;

            TextWriter sw = new StringWriter(CultureInfo.InvariantCulture);
            XmlTextWriter tw = new DevTeam.DataManager.KarbelXmlWriter(sw);
            dt.WriteXml(tw, XmlWriteMode.IgnoreSchema);
            tw.Close();
            return sw.ToString();

        }

        public static string Serialize(DataTable dataTable, string tableName, string datasetName)
        {
            dataTable.TableName = tableName;
                
            foreach (DataColumn column in dataTable.Columns)
                if (column.DataType == typeof(DateTime))
                    column.DateTimeMode = DataSetDateTime.Unspecified;

            if (dataTable.DataSet == null)
            {
                DataSet ds = new DataSet(datasetName);
                ds.Tables.Add(dataTable);
            }
            else
                dataTable.DataSet.DataSetName = datasetName;

            
            TextWriter sw = new StringWriter(CultureInfo.InvariantCulture);
            XmlTextWriter tw = new DevTeam.DataManager.KarbelXmlWriter(sw);
            dataTable.WriteXml(tw, XmlWriteMode.IgnoreSchema);
            tw.Close();
            return sw.ToString();
        }


    }
}